package com.example.acceledemo;

import java.io.Serializable;
import android.app.Activity;

import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;

public class MapActivity extends Activity {

	private boolean isRecording = false;

	private ImageButton button;
	private AnimationDrawable recAnimation;
	private WakeLockManager wLockManager;

	private AcceleSensor acceleSensor;
	private AlertManager alertManager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		Log.d("location", "onCreate()");
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		alertManager = new AlertManager(this);
		acceleSensor = new AcceleSensor(this);
		wLockManager = new WakeLockManager(this);

		setUpView();

	}

	private void setUpView() {

		button = (ImageButton) findViewById(R.id.recButton);
		recAnimation = (AnimationDrawable) button.getBackground();

	}

	private void startRecording() {
		
		acceleSensor.setAcceleRoute(new AcceleRoute());
		acceleSensor.startUsingAccele();
		
		setRecording(true);
		Log.d("location", "startRecording()");
		
		button.setVisibility(ImageView.VISIBLE);
		recAnimation.start();

		wLockManager.acquire("startRecording()");
	}

	protected void stopRecording() {
		setRecording(false);
		Log.d("location", "stopRecording()");
		
		button.setVisibility(View.GONE);
		recAnimation.stop();

		acceleSensor.stopUsingAccele();
		acceleSensor.setAcceleRoute(null);

		wLockManager.release("stopRecording()");

	}

	private void continueRecording() {
		Log.d("location", "continueRecording()");
		
		button.setVisibility(ImageView.VISIBLE);
		recAnimation.start();
		
		acceleSensor.startUsingAccele();
		wLockManager.acquire("continueRecording()");

	}

	@Override
	protected void onPause() {
		Log.d("location", "onPause()");
		super.onPause();

		acceleSensor.stopUsingAccele();
		wLockManager.release("onPause()");

	}

	@Override
	protected void onResume() {

		Log.d("location", "onResume()");

		if (isRecording)
			continueRecording();

		Toast.makeText(this, "On Resume()::isRecording:" + isRecording, Toast.LENGTH_SHORT).show();

		super.onResume();
	}

	protected void setRecording(boolean isRecording) {
		this.isRecording = isRecording;
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {

		Log.d("location", "onSaveInstanceState()");
		outState.putBoolean("isRecording", isRecording);
		outState.putSerializable("acceleRoute", acceleSensor.getAcceleRoute());

		super.onSaveInstanceState(outState);
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {

		Log.d("location", "onRestoreInstanceState()");

		boolean isRec = savedInstanceState.getBoolean("isRecording");
		setRecording(isRec);

		Serializable acceleRoute = savedInstanceState
				.getSerializable("acceleRoute");
		acceleSensor.setAcceleRoute((AcceleRoute) acceleRoute);

		super.onRestoreInstanceState(savedInstanceState);
	}

	@Override
	protected void onUserLeaveHint() // before onPause, home button
	{
		Log.d("location", "onUserLeaveHint()");
		super.onUserLeaveHint();
	}

	@Override
	protected void onDestroy() {
		Log.d("location", "onDestroy()");
		super.onDestroy();
	}

	@Override
	public void onBackPressed() {

		if (isRecording)
			alertManager.showCancelQuestion("BackPressed");
		else
			super.onBackPressed();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.map_activity, menu);

		menu.findItem(R.id.anuluj_trase_button).setVisible(false);
		menu.findItem(R.id.zakoncz_trase_button).setVisible(false);

		if (isRecording) {
			menu.findItem(R.id.nowa_trasa_button).setVisible(false);
			menu.findItem(R.id.zakoncz_trase_button).setVisible(true);
			menu.findItem(R.id.anuluj_trase_button).setVisible(true);
		}

		return true;
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {

		if (isRecording) {
			menu.findItem(R.id.nowa_trasa_button).setVisible(false);
			menu.findItem(R.id.zakoncz_trase_button).setVisible(true);
			menu.findItem(R.id.anuluj_trase_button).setVisible(true);
		} else {
			menu.findItem(R.id.nowa_trasa_button).setVisible(true);
			menu.findItem(R.id.zakoncz_trase_button).setVisible(false);
			menu.findItem(R.id.anuluj_trase_button).setVisible(false);
		}

		return super.onPrepareOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {

		case R.id.nowa_trasa_button:

			startRecording();
			return true;

		case R.id.zakoncz_trase_button:
			alertManager.showStopRecordingQuestion(acceleSensor.getAcceleRoute());
			//else
				//Toast.makeText(this, "No data to write", Toast.LENGTH_SHORT).show();
			return true;

		case R.id.anuluj_trase_button:
			alertManager.showCancelQuestion("Anuluj");
			return true;

		case R.id.ustwienia_button:
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	public void onMyLocationButtonPressed(View v) {
		Toast.makeText(this, "Siemka nie ma lokalizacji", Toast.LENGTH_LONG)
				.show();
	}

	public void onRecordingButtonPressed(View v) {

	}

	public AcceleSensor getAcceleSensor() {
		return acceleSensor;
	}

}
